<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        #container {
            width: 800px;
            height: 600px;
            border: 1px solid black;
            margin: 50px 50px;
        }

        #box {
            width: 200px;
            height: 200px;
            background-color: aqua;
            position: absolute;
            left: 50px;
            top: 50px;
        }
    </style>
</head>

<body>
    <!-- <p id="pMouse">光标位置</p> -->
    <!-- <script>
        // ·实现页面实时显示光标坐标位置
        document.onmousemove = function(e){
            pMouse.innerText = `当前鼠标位置：(${e.pageX},${e.pageY})`
        }
    </script> -->

    <div id="container">
        <div id="box"></div>
    </div>

    <script>
        var isDragging = false
        var box = document.querySelector("#box")

        box.onmousedown = function (e) {
            isDragging = true
        }
        document.onmouseup = function (e) {
            isDragging = false
        }
        document.onmousemove = function (e) {
            if (isDragging) {
                // box.style.left = (e.pageX < 50 ? 50 : e.pageX) + "px"
                // box.style.top = (e.pageY < 50 ? 50 : e.pageY) + "px"

                // box.style.left = (e.pageX > 650 ? 650 : e.pageX) + "px"
                // box.style.top = (e.pageY > 450 ? 450 : e.pageY) + "px"

                var x = e.pageX
                x = x < 50 ? 50 : x
                x = x > 650 ? 650 : x
                box.style.left = x + "px"

                var y = e.pageY
                y = y < 50 ? 50 : y
                y = y > 450 ? 450 : y
                box.style.top = y + "px"
            }
        }
    </script>
</body>

</html>